//
//  ViewController2.m
//  逆向传值(通知)
//
//  Created by whz on 2017/7/10.
//  Copyright © 2017年 王鸿政_whz. All rights reserved.
//

#import "ViewController2.h"

@interface ViewController2 () <UITextFieldDelegate>

@property(nonatomic, strong)UITextField *textField2;
@property(nonatomic, strong)UIButton *btn2;

@end

@implementation ViewController2

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor colorWithRed:231 / 255.0 green:231 / 255.0 blue:231 / 255.0 alpha:1.0];
    
    [self createUI];
}

- (void)createUI {
    
    self.textField2 = [[UITextField alloc] initWithFrame:CGRectMake(self.view.center.x-50, self.view.center.y-200, 100, 50)];
    [self.view addSubview:_textField2];
    _textField2.text = _str2;
    _textField2.textColor = [UIColor blackColor];
    _textField2.textAlignment = NSTextAlignmentCenter;
    _textField2.font = [UIFont systemFontOfSize:15 weight:2];
    _textField2.borderStyle = UITextBorderStyleLine; // 边框样式
    _textField2.placeholder = @"请输入信息"; // 水印提示
    [_textField2 setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"]; // 水印提示字体颜色
    [_textField2 setValue:[UIFont boldSystemFontOfSize:15] forKeyPath:@"_placeholderLabel.font"]; // 水印提示字体大小
    _textField2.clearButtonMode = UITextFieldViewModeWhileEditing; // 编辑时出现
    //    _textField2.clearButtonMode = UITextFieldViewModeAlways; // 输入框的叉号
    //    _textField2.secureTextEntry = YES; // 每输入一个字符就变成点，用语密码输入
    _textField2.keyboardType = UIKeyboardTypeNamePhonePad; // 更换键盘样式
    _textField2.returnKeyType = UIReturnKeySend; // return键变成什么键
    _textField2.keyboardAppearance = UIKeyboardAppearanceAlert; // 键盘外观设置
    _textField2.delegate = self; // 设置代理用于实现键盘收回
    
    self.btn2 = [[UIButton alloc] initWithFrame:CGRectMake(self.view.center.x-50, self.view.center.y-100, 100, 50)];
    [self.view addSubview:_btn2];
    [_btn2 setTitle:@"逆传" forState:UIControlStateNormal];
    [_btn2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    _btn2.titleLabel.textAlignment = NSTextAlignmentCenter;
    _btn2.titleLabel.font = [UIFont systemFontOfSize:20];
    _btn2.layer.cornerRadius = 10.0; // 设置按钮边框圆角弧度
    _btn2.layer.borderColor = [UIColor orangeColor].CGColor; // 设置边框颜色
    _btn2.layer.borderWidth = 2.0f; // 设置边框宽度
    [_btn2 addTarget:self action:@selector(returnVc1) forControlEvents:UIControlEventTouchUpInside];
}

- (void)returnVc1 {
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"sendValue" object:nil userInfo:@{@"value":_textField2.text}]; // 发送通知
    
    /*
     键盘回弹方式
     */
    _textField2 = self.view.window.subviews[0];// 一.设置textField的enabled属性为NO。//找到编译窗口
    _textField2.enabled = !_textField2.enabled;
//    [_textField2 endEditing:YES]; // 二.让textField处于非编辑状态
//    [_textField2 resignFirstResponder]; // 三.让textField取消第一响应者 // [textField becomeFirstResponder];// 成为第一响应者
//    [self.view.window endEditing:YES]; // 四.让window处于非编辑状态
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

/**
 键盘收回代理
 */
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    [textField resignFirstResponder]; //主要是[receiver resignFirstResponder]在哪调用就能把receiver对应的键盘往下收
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
